﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Windows.Forms;
using System.Data;

namespace PatientsManagement.Database
{
    class UsersData
    {
        static UsersDataContext users = new UsersDataContext();

        public static List<String> GetUserNames()
        {
            return (from user in users.GetTable<User>()
                    select user.Username).ToList<String>();
        }

        public static string GetUserPassword(string username)
        {
            return (from user in users.GetTable<User>()
                    where user.Username ==username
                    select user.Password).ToList<String>()[0];
        }

        public static string GetUserForenameSurname(string login)
        {
            string name = "";
            if ((from user in users.GetTable<User>()
                 where (user.Username == login)
                 select user.Forename).Count() > 0)
            {
                name += (from user in users.GetTable<User>()
                         where (user.Username == login)
                         select user.Forename).ToList<string>()[0];
                name += " " + (from user in users.GetTable<User>()
                               where (user.Username == login)
                               select user.Surname).ToList<string>()[0];
                return name;
            }
            else
                return "notexistingusername";
        }

        public static bool IsAdmin(string userName)
        {
            int value;
            value = (from user in users.GetTable<User>()
                     where (user.Username == userName)
                     select user.Type_ID).ToList<int>()[0];
            if (value == 1)
                return true;
            return false ;
        }

        public static bool IsActive(string userName)
        {
            var query = (from user in users.Users
                         where user.Username.ToLower() == userName.ToLower()
                         select user).Single();
            return query.Active;
        }

        public static string GetUserType(string userName)
        {
            var query = (from user in users.Users
                         join type in users.User_types on user.Type_ID equals type.Type_ID
                         where user.Username.ToLower() == userName.ToLower()
                         select user).Single();
            return query.User_type.Type_name;
        }

        public static int GetUserID(string userName)
        {
            var query = (from user in users.Users
                         where user.Username.ToLower() == userName.ToLower()
                         select user).Single();
            return query.UserID;
        }

        public static List<string> UserTypes()
        {
            List<string> types;
            types = (from type in users.GetTable<User_type>()
                        select type.Type_name).ToList<string>();
            return types;
        }

        private static int GetTypeID(string typeName)
        {
            return (from type in users.GetTable<User_type>()
                        where type.Type_name == typeName
                        select type.Type_ID).ToList<int>()[0];
        }

        public class UsersInformation
        {
            public int ID { get; set; }
            public string Nazwa { get; set; }
            public string Imię { get; set; }
            public string Nazwisko { get; set; }
            public string Typ { get; set; }
            public bool Aktywny { get; set; }
            public bool Powiązany { get; set; }
        }

        public static IQueryable<UsersInformation> GetUsersTable()
        {
            var query = (from user in users.Users
                         join type in users.User_types on user.Type_ID equals type.Type_ID
                         select new UsersInformation()
                         { 
                             ID = user.UserID, 
                             Nazwa = user.Username,
                             Imię = user.Forename,
                             Nazwisko = user.Surname,
                             Typ = type.Type_name ,
                             Aktywny = user.Active,
                             Powiązany = user.Linked
                         });
            return query;
        }

        public static void UpdateUsersTable(List<string>usernames,List<bool>activeStatus)
        {
            
            for (int i = 0; i < usernames.Count; i++)
            {
                var result = (from user in users.Users
                              where user.Username == usernames[i]
                              select user).Single();
                result.Active = activeStatus[i];
            }
            users.SubmitChanges();
        }

        public static void AddUser(string username, string password, string forename, string surname, string type, bool isActive)
        {
            User user = new User
            {
                Username = username.ToLower(),
                Password = HelperMethods.HashPassword(password),
                Forename = forename,
                Surname = surname,
                Type_ID = GetTypeID(type),
                Active = isActive
            };
            users.Users.InsertOnSubmit(user);
            try
            {
                users.SubmitChanges();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        public static void EditUser(string username, string password, string forename, string surname, string type)
        {
            var singleUser = (from user in users.Users
                        where user.Username == username
                        select user).Single();
            if (password != "")
                singleUser.Password = HelperMethods.HashPassword(password);
            singleUser.Forename = forename;
            singleUser.Surname = surname;
            singleUser.Type_ID = GetTypeID(type);
            try
            {
                users.SubmitChanges();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        public static void DeleteUser(string username)
        {
            var singleUser = (from user in users.Users
                                where user.Username.ToLower() == username.ToLower()
                                select user).Single();
            users.Users.DeleteOnSubmit(singleUser);
            try
            {
                users.SubmitChanges();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

    }
}
